package com.lagou.dao.impl;

import com.lagou.pojo.Course;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDaoimpl  implements CourseDao {
    //操作数据库
    //重写接口方法

    @Override
    public List<Course> findCourseList() {
//select * from course where id=? and status=?
        //parans 可以是一个一个参数 也可以是数组【xx,xx,xx】
//queryRunner(数据库连接池对象)
       //query (SQL.new BeanListHandler<Course>(Course.class), )  查询 结果集
       //update(sql,parans) 更新 修改 删除 返回影响的行数 int 2
        try {
            //查询数据库course
            //创建 QueryRunner
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //sql
            String sql = "\n" + "\n" +
                    "select id,course_name ,brief,price,sort_num,STATUS from course where is_del=0;";
            //query 方法进行查询
            List<Course> query = qr.query(sql, new BeanListHandler<Course>(Course.class), 0);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    //实现条件查询接口

    @Override
    public List<Course> findCourseNameAndStatus(String course_name, String status) {


        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            //sql语句
            //select id,course_name ,brief,price,sort_num,STATUS from course where is_del=0;and course_name like "%java%" and STATUS =0;

            StringBuffer sb = new StringBuffer("select id,course_name ,brief,price,sort_num,STATUS from course where is_del=0;");
            List<Object> List = new ArrayList<>();
            List.add(0);

            if (course_name!=null){
                //

                sb.append(" and course_name like ?");
                course_name="%"+course_name+"%";
                List.add(course_name);//[0,服务]

            }else if (status!=null){
                sb.append("and STATUS =?");
                int i =Integer.parseInt(status);
                List.add(i);//[0,服务]
            }

            List<Course> query = null;
            query = qr.query(sb.toString(), new BeanListHandler<Course>(Course.class),List.toString());
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }
    //新建查询


    @Override
    public int saveCourseSales(Course course) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql ="INSERT INTO course(\n" +
                    "course_name,\n" +
                    "brief,\n" +
                    "teacher_name,\n" +
                    "teacher_info,\n" +
                    "preview_first_field,\n" +
                    "preview_second_field,\n" +
                    "discounts,\n" +
                    "price,\n" +
                    "price_tag,\n" +
                    "share_image_title,\n" +
                    "share_title,\n" +
                    "share_description,\n" +
                    "course_description,\n" +
                    "course_img_url,\n" +
                    "STATUS,\n" +
                    "create_time,\n" +
                    "update_time\n" +
                    ")VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";

            Object[] params ={
                    course.getCourse_name(),course.getBrief(),course.getTeacher_name(),course.getTeacher_info(),course.getPreview_first_field(),
                    course.getPreview_second_field(),course.getDiscounts(),course.getPrice(),course.getPrice_tag(),
                    course.getSales(),course.getShare_description(),course.getShare_title(),course.getCourse_description(),course.getCourse_img_url(),course.getStatus(),
                    course.getCreate_time(),course.getUpdate_time()
            };
            int num = qr.update(sql, params);
            return  num;
        } catch (SQLException e) {
            e.printStackTrace();
            return 1;
        }
    }
}


